// components/searchBar/searchBar.js
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    placeholder: {
      type: String,
      value: '可搜索购买者手机号'
    },
    orderStatus: {
      type: Array
    },
    showStatus: {
      type: Boolean,
      value: false
    },
    showTreatment: {
      type: Boolean,
      value: false
    },
    timeTitle: {
      type: String,
      value: '时间筛选'
    },
    doctorsTitle: {
      type: String,
      value: '责任人筛选'
    },
    doctorsPlaceholder: {
      type: String,
      value: '请选择责任人姓名'
    }
  },
  lifetimes: {
    ready() {
      const _this = this
      app.on('search', this, function (value) {
        _this.setData({
          value: value
        })
      })
      wx.request({
        url: app.globalData.serverUrl + '/staff/list',
        method: 'GET',
        header: {
          'token': wx.getStorageSync('token')
        },
        success:(res=>{
         let resData = res.data
         if(resData.code==200){
           console.log(resData.data.staffList);
           resData.data.staffList.map(item=>{
             item.label = item.staffName
             item.value = item.staffId
           })
           _this.setData({
             options:resData.data.staffList
           })
         }
        })
      })
    },

  },
  /**
   * 组件的初始数据
   */
  data: {
    showFilter: false,
    showFilters: false,
    status: '',
    startDate: '开始日期',
    endDate: '结束日期',
    startDate1: "",
    endDate1: "",
    startDate2: "",
    endDate2: "",
    doctors: "",
    medicalActive: false,
    treatment: '',
    treatmentActive: false,
    options: [
    ],
    bottom: '',
    treatmentBottom: '',
    value: '',
    doctorsId:''
  },
  /**
   * 组件的方法列表
   */

  methods: {
    bindfocus() {
      let arr = []
      this.data.orderStatus.map(item=>{
        if(item.selected){
          arr.push(item)
        }
      })
      if (!this.data.showFilter) {
        if(this.data.startDate!='开始日期' || this.data.endDate!='结束日期' || arr.length>0 ||  this.data.doctorsId!=''){
          wx.showToast({
            title: '请先清空筛选条件',
            icon: 'none'
          })
          return
        }
        console.log(this.data.value);
        wx.navigateTo({
          url: '/pages/search/search?placeholder=' + (this.data.value?this.data.value:this.data.placeholder),
        })
      } else {
        wx.showToast({
          title: '请先清空筛选条件',
          icon: 'none'
        })
      }
    },
    bindtapFilter() {
     if(this.data.value!=''){
       return wx.showToast({
        title: '请先清空搜索内容',
        icon: 'none'
      })
     }
      this.setData({
        showFilter: !this.data.showFilter,
        showFilters: true
      })
      let _this = this
      wx.createSelectorQuery().in(this).select('#medical').boundingClientRect(function (rect) {
        _this.setData({
          bottom: rect?.bottom
        })
      }).exec()
      wx.createSelectorQuery().in(this).select('#treatment').boundingClientRect(function (rect) {
        _this.setData({
          treatmentBottom: rect?.bottom
        })
      }).exec()
    },
    bindtapStatus(e) {
      // 多选
      let index = e.currentTarget.dataset.index
      let orderStatus = this.data.orderStatus
      let seleced = !orderStatus[index].selected
      orderStatus[index].selected = seleced
      let count = 0
      this.data.orderStatus.forEach(item => {
        if (item.selected) {
          count = count + 1
        }
      })
      this.setData({
        orderStatus: orderStatus,
        checked: count == orderStatus.length
      })
      // 单选
      // let status = e.currentTarget.dataset.status
      // if (status == this.data.status) {
      //   this.setData({
      //     status: ''
      //   })
      // } else {
      //   this.setData({
      //     status: status
      //   })
      // }
    },
    bindStartDateChange(e) {
      this.setData({
        startDate2: e.detail.value,
        startDate: e.detail.value
      })
    },
    bindEndDateChange(e) {
      this.setData({
        endDate1: e.detail.value,
        endDate: e.detail.value
      })
    },
    bindtapMedical() {
      this.setData({
        treatmentActive: false,
        medicalActive: !this.data.medicalActive
      })
    },
    bindChangeMedical(e) {
      this.setData({
        doctors: e.detail.label,
        doctorsId: e.detail.value,
        medicalActive: false
      })
    },
    bindtapTreatment() {
      this.setData({
        medicalActive: false,
        treatmentActive: !this.data.treatmentActive
      })
    },
    bindChangeTreatment(e) {
      this.setData({
        treatment: e.detail.value,
        treatmentActive: false
      })
    },
    /**
     * 用户点击重置
     */
    bindtapReset() {
      let acrr = this.data.orderStatus
      acrr.map(item=>{
        item.selected = false
      })
      this.setData({
        status: '',
        startDate: '开始日期',
        endDate: '结束日期',
        startDate1: '',
        endDate1: '',
        doctors: '',
        doctorsId:'',
        statusArr:[],
        orderStatus:acrr
      })
      this.triggerEvent('change', {
        status: '',
        startDate: '',
        endDate: '',
        doctors: '',
        doctorsId:'',
        statusArr:[]
      })
    },
    /**
     * 用户点击确定
     */
    bindtapConfirm() {
      let statusArr = []
      this.data.orderStatus.map(item=>{
        if(item.selected){
          statusArr.push(item.value)
        }
      })
      const {
        status,
        startDate,
        endDate,
        doctors,
        doctorsId
      } = this.data
      this.triggerEvent('change', {
        status: status,
        startDate: startDate,
        endDate: endDate,
        doctors: doctors,
        statusArr:statusArr,
        doctorsId:doctorsId
      })
      if(this.data.startDate!='开始日期' || this.data.endDate!='结束日期' || statusArr.length>0 ||  this.data.doctorsId!=''){
        this.setData({
          showFilters: true
        })
      }else{
        this.setData({
          showFilters: false
        })
      }
      this.setData({
        showFilter: false
      })
    }
  }
})